const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
      earnings: [{
        price: '',
        name: '累计获得奖励'
      }, {
        price: '',
        name: '已结算金额'
      }, {
        price: '',
        name: '未结算金额'
      }],
      earningsPrice: ["total_reward", "already_settled", "not_settled"],
      orderSelection:[{title:'全部',name:"all"},{title:'已结算',name:1},{title:'未结算',name:0}],//订单筛选
      activeName: '',
      listOfTime:[],//时间列表
      orderList:[],//订单列表
      orderPage:1, //订单列表当前页
      page:1,//时间列表当前页
      orderLastPage:false,//订单是否最后一页
      labelStatus:'unselected' //标签状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },

  //获取数据
  getData(type) {
    
    let json = {
      page:this.data.page
    }
    if(type != "all"&&type != undefined){
      json["settlement_status"] = type
    }
    let urlStr = app.getNetAddresss("plugin.difference-reward.frontend.reward-log.get-reward-list");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let arry =  this.data.earnings
          this.data.earnings.forEach((item,index) => {
              arry[index].price = res.data.reward_summarize[this.data.earningsPrice[index]]
           })
          this.setData({
            username:res.data.member_info.username,
            avatar_image:res.data.member_info.avatar_image,
            listOfTime:res.data.list,
            earnings:arry
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },


  //获取月份展开订单数据
  getOrderList(month,type) {
    if(typeof(month) == "number"){
      month = this.data.listOfTime[month].sub_month_str.replace("-","")
    }

    if(type != "more"){
      this.setData({
        orderPage:1
      })
    }
    if(!month){
      return
    }
    let json = {
      month_str:month.replace("-",""),
      page:this.data.orderPage,
    }
    if(this.data.labelStatus!= 'unselected'&&this.data.labelStatus.detail.name != "all"){
      json["settlement_status"] = this.data.labelStatus.detail.name
    }
    let urlStr = app.getNetAddresss("plugin.difference-reward.frontend.reward-log.get-reward-list-to-month");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
            this.setData({
              orderLastPage:false
            })
            if(res.data.list.current_page>=res.data.list.last_page){ //判断订单列表是否为最后一页
              this.setData({
                orderLastPage:true
              })
            }
            if(type == "more"){ //如果是查看更多数组值累加
              this.setData({
                 orderList : [...this.data.orderList,...res.data.list.data]
              });
              console.log("9999",this.data.orderList)
            }else{
              this.setData({
                orderList:res.data.list.data
              })
            }
            this.setData({
              pagingChoose:res.data.list
            })
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },

  //查看更多
  readMore(e){
    let page = this.data.orderPage
    page++
    this.setData({
      orderPage:page
    })
    this.getOrderList(e.currentTarget.dataset.sub_month_str,"more")
  },

  clickChange(e){ //标签页改变
    this.getData(e.detail.name)
    this.setData({
      activeName: '',
      labelStatus:e
    });
  },

  foldChange(event) { //折叠面板改变
    this.setData({
      activeName: event.detail,
    });
    if(event.detail == ""){
      event.detail = 0
    }
    this.getOrderList(event.detail)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  }
})